package com.example.his.api.mis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.his.api.db.dao.RuleDao;
import com.example.his.api.db.pojo.RuleEntity;
import com.example.his.api.mis.controller.form.SearchRuleByPageForm;
import com.example.his.api.mis.service.RuleService;
import com.example.his.api.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RuleServiceImpl extends ServiceImpl<RuleDao, RuleEntity> implements RuleService {
    @Override
    public List<Map<String, Object>> searchAllRule() {
        return this.listMaps();
    }

    @Override
    public PageUtils searchByPage(SearchRuleByPageForm form) {
        Page<HashMap> page = PageHelper.startPage(form.getPage(), form.getLength())
                .doSelectPage(() -> baseMapper.searchByPage(form));
        return new PageUtils(page.getResult(),page.getTotal(),form.getPage(),form.getLength());
    }

    @Override
    public int myInsert(RuleEntity entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public HashMap searchById(Integer id) {
        RuleEntity ruleEntity = baseMapper.selectById(id);
        return (HashMap) BeanUtil.beanToMap(ruleEntity);
    }

    @Override
    public int myUpdate(RuleEntity entity) {
        return baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public int myDeleteById(Integer id) {
        return baseMapper.myDeleteById(id);
    }


}
